package com.toilet.easyvideofx.controller;

import com.toilet.easyvideofx.constant.SetConfigEnum;
import com.toilet.easyvideofx.model.SetConfig;
import com.toilet.easyvideofx.theme.SamplerTheme;
import com.toilet.easyvideofx.theme.ThemeManager;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListView;
import javafx.scene.control.SplitPane;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.util.StringConverter;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Objects;
import java.util.ResourceBundle;

/**
 * 设置控制器
 */
public class SetController extends StackPane implements Initializable {
    public AnchorPane setPane;
    public SplitPane splitPane;
    public VBox leftContent;
    public VBox rightContent;
    public ListView<SetConfig> setListView;

    private static final ThemeManager TM = ThemeManager.getInstance();

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        setPane.widthProperty().addListener((observable, oldValue, newValue) -> {
            splitPane.setPrefWidth(newValue.doubleValue());
        });

        SetConfig globalTheme = new SetConfig(1L, "全局主题");
        ObservableList<SetConfig> strings = FXCollections.observableArrayList(globalTheme);
        setListView.setItems(strings);
        leftContent.widthProperty().addListener((observable, oldValue, newValue) -> {
            setListView.setPrefWidth(leftContent.getWidth());
        });
    }

    /**
     * 打开设置页面
     *
     * @param mouseEvent
     */
    @FXML
    public void handleMouseClick(MouseEvent mouseEvent) {
        SetConfig selectedItem = setListView.getSelectionModel().getSelectedItem();
        if (Objects.equals(SetConfigEnum.global_theme.getId(), selectedItem.getId())) {
            showGlobalThemePage();
        }
    }

    /**
     * 展示全局主题页面
     */
    private void showGlobalThemePage() {
        //进来之前先清除容器内容
        rightContent.getChildren().clear();
        //主题颜色
        HBox colorThemeBox = new HBox();
        Text text = new Text("主题颜色");
        ChoiceBox<SamplerTheme> themeSelector = createThemeSelector();
        colorThemeBox.getChildren().addAll(text, themeSelector);

        rightContent.getChildren().addAll(colorThemeBox);
    }

    /**
     * 创建主题选择器
     *
     * @return
     */
    private ChoiceBox<SamplerTheme> createThemeSelector() {
        var choiceBox = new ChoiceBox<SamplerTheme>();
        choiceBox.setPrefWidth(200);

        List<SamplerTheme> themes = TM.getRepository().getAll();
        choiceBox.getItems().setAll(themes);
        // 设置初始化主题
        SamplerTheme currentTheme = Objects.requireNonNullElse(TM.getTheme(),
                TM.getDefaultTheme());
        themes.stream()
                .filter(t -> Objects.equals(currentTheme.getName(), t.getName()))
                .findFirst()
                .ifPresent(t -> choiceBox.getSelectionModel().select(t));
        // 设定初始化值
        choiceBox.getSelectionModel().selectedItemProperty().addListener((obs, old, val) -> {
            if (val != null && getScene() != null) {
                TM.setTheme(val);
            }
        });
        choiceBox.setConverter(new StringConverter<>() {
            //自定义展示主题
            @Override
            public String toString(SamplerTheme theme) {
                return theme != null ? theme.getName() : "";
            }

            @Override
            public SamplerTheme fromString(String themeName) {
                return TM.getRepository().getAll().stream()
                        .filter(t -> Objects.equals(themeName, t.getName()))
                        .findFirst()
                        .orElse(null);
            }
        });
        return choiceBox;
    }
}
